<template>
  <div v-loading="loading">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px" class="dataForm">
      <el-row>
        <el-col :span="12">
          <el-form-item :label="$t('客户类型')" prop="investType">
            <dictSelect
              v-model="form.investType"
              :clearable="true"
              dictionary="lp_type"
              @change="changeType"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('客户名称')" prop="investIdList">
            <el-select v-model="form.investIdList" :multiple="true" filterable :placeholder="$t('客户名称')" clearable>
              <div style="position: absolute;right: 10px;bottom: 10px;z-index: 2001;">
                <el-button size="mini" @click="choiceAll">{{ $t('全选') }}</el-button>
                <el-button size="mini" @click="form.investIdList = []">{{ $t('取消') }}</el-button>
              </div>
              <el-option
                v-for="dict in lpBaseOption"
                :key="dict.name"
                :label="dict.name"
                :value="dict.id"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('备注')" prop="remark">
            <el-input v-model="form.remark" type="textarea" :placeholder="$t('备注')" show-word-limit maxlength="1000" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer" style="text-align: right;">
      <!--保存 取消按钮-->
      <save-btn has-permi="lp:investigation" />
    </div>
  </div>
</template>

<script>
import { getLpBaseByType } from '@/api/fund/questionnaire/settings/questionnaireQuestion/lpQuestionnaireQuestion'
import { saveDataBySetIdAndInvestId } from '@/api/fund/questionnaire/investigation/lpQuestionnaireInvestigation'

export default {
  components: {},
  data() {
    return {
      // 表单参数
      form: {},
      // 遮罩层
      loading: false,
      // 是否隐藏其它按钮
      otherBtnHide: false,
      lpBaseOption: [],
      // 表单校验
      rules: {
        investType: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }],
        investIdList: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }]
      }
    }
  },
  created() {
  },
  methods: {
    // 初始化数据
    init(data) {
      this.form.dataId = data.id
    },
    // 表单重置
    reset() {
      this.form = {
        investType: undefined,
        investIdList: undefined,
        dataId: undefined,
        remark: undefined
      }
      this.resetForm('form')
    },
    // 取消按钮
    cancel() {
      this.$parent.$parent.invitationOpen = false
      this.$parent.$parent.reset()
    },
    // 关闭form页面
    closeForm() {
      // 关闭form页面遮罩层
      this.loading = false
      // 关闭form页面
      if (this.$parent.$parent.invitationOpen !== undefined) {
        this.$parent.$parent.invitationOpen = false
      }
      // 刷新list页面
      if (this.$parent.$parent.getList !== undefined) {
        this.$parent.$parent.getList()
      }
    },
    changeType() {
      this.$set(this.form, 'investIdList', undefined)
      getLpBaseByType(this.form.investType).then(response => {
        this.lpBaseOption = response.data
      })
    },
    /** 项目全选 */
    choiceAll() {
      this.form.investIdList = []
      this.lpBaseOption.forEach(item => {
        this.form.investIdList.push(item.id)
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          this.loading = true
          saveDataBySetIdAndInvestId(this.form).then(response => {
            console.log(response)
            this.closeForm()
          }).catch(() => {
            this.loading = false
          })
        } else {
          this.locationError(document)
        }
      }).catch(() => {
        this.loading = false
      })
    }
  }
}
</script>

<style lang="scss" scoped>
/deep/ .el-select-dropdown__item {
  position: unset;
}
</style>
